What is form-urlencoded?
The form-urlencoded npm package is used to encode and decode URL-encoded form data. It is particularly useful for working with HTTP requests and responses where data needs to be sent or received in the application/x-www-form-urlencoded format.
What are form-urlencoded's main functionalities?
Encoding an Object to URL-encoded String
This feature allows you to convert a JavaScript object into a URL-encoded string, which is useful for sending data in HTTP POST requests.
const formurlencoded = require('form-urlencoded');
const data = { name: 'John Doe', age: 30, city: 'New York' };
const encoded = formurlencoded(data);
console.log(encoded); // Output: 'name=John%20Doe&age=30&city=New%20York'
Decoding a URL-encoded String to an Object
This feature allows you to convert a URL-encoded string back into a JavaScript object, which is useful for processing data received in HTTP POST requests.
const formurlencoded = require('form-urlencoded');
const encoded = 'name=John%20Doe&age=30&city=New%20York';
const decoded = formurlencoded.parse(encoded);
console.log(decoded); // Output: { name: 'John Doe', age: '30', city: 'New York' }
Other packages similar to form-urlencoded
qs
The qs package is a query string parser and stringifier with support for nested objects. It provides more advanced features compared to form-urlencoded, such as handling nested objects and arrays.
querystring
The querystring package is a Node.js core module that provides utilities for parsing and formatting URL query strings. It is similar to form-urlencoded but is built into Node.js and may not have as many features.
urlencode
The urlencode package is a simple utility for URL encoding and decoding. It is similar to form-urlencoded but focuses solely on encoding and decoding without additional features.
form-urlencoded
(c)Bumblehead, JBlashill
Returns 'x-www-form-urlencoded' string data, an encoding often used when an HTML form is submitted. Form data is serialised in this format and sent to a server.
import formurlencoded from 'form-urlencoded';
const obj = {
str : 'val',
num : 0,
arr : [3, {prop : false}, 1, null, 6],
obj : {prop1 : null, prop2 : ['elem']}
};
console.log(formurlencoded(obj));
console.log(formurlencoded(obj, {
ignorenull : true,
skipIndex : true,
sorted : true
}));
console.log(formurlencoded(obj, {
ignorenull : true,
useDot : true,
skipIndex : true,
skipBracket : true
}));
changelog
- 4.5.0, add exports to package.json to support native esm
(The MIT License)
Copyright (c) Bumblehead chris@bumblehead.com
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the 'Software'), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.